const Router = require("@koa/router");
const router = new Router();
const { modelDailyrecom, modelFourTravel } = require("@/models/collection-pc");
const { Resreturn, Classifytravels } = require("@/config/valiData");
const { Auth } = require("@/token/auth");
const { gentoken } = require("@/token/jwt");
const moment = require("moment");
moment.locale("zh-cn");
const { modelHometab, modelArticle } = require("@/models/collection");
const { looKup, looKupRecommend } = require("@/config/lookup");
const fs = require("fs");

//获取小程序端顶部每日推荐游记
router.get("/wxGainDailyRecom", async (ctx) => {
  const { page } = ctx.query;
  Resreturn(page);
  const res = await modelDailyrecom
    .find(
      {},
      {
        time: false,
        timestamp: false,
      }
    )
    .sort({ timestamp: -1 })
    .skip((page - 1) * 1)
    .limit(1);

  //获取总条数
  const count = await modelDailyrecom.countDocuments();
  const resData = { data: res, count };
  ctx.send("SUCCESS", 200, resData);
});

//获取小程序端四个游记推荐
router.get("/wxGainRecomTravel", async (ctx) => {
  const res = await modelFourTravel.aggregate([
    { $sort: { timestamp: -1 } }, //按创建时间倒序查询
    looKupRecommend().model_Article,
    looKupRecommend().model_reco_user,
    { $unwind: "$articleData" },
    { $unwind: "$userData" },
    {
      $project: {
        _id: 1,
        imageUrl: 1,
        travel_id: 1,
        title: "$articleData.title",
        nickname: "$userData.nickname",
        avatarUrl: "$userData.avatarUrl",
        address: "$articleData.address",
        fileType: "$articleData.fileType",
      },
    },
  ]);
  ctx.send("SUCCESS", 200, res);
});

//小程序端首页10个游记分类
router.get("/recomm-travel", async (ctx) => {
  const res = await modelHometab.find({});
  ctx.send("SUCCESS", 200, res);
});

//导入10个游记分类到数据库  仅限一次使用：后台
// router.get('/hometab-database',async ctx=>{
//     const hometab = fs.readFileSync('C:/Users/24599/Desktop/学习资料/lvyou-node/hometab.json')
//     const res = JSON.parse(hometab)
//     await modelHometab.insertMany(res) //插入数据
// })

//首页游记分类
router.get("/travelogue-class", async (ctx) => {
  const res = moment().utcOffset(8).format("M");
  const arr = [
    { name: "推荐", key: "001" },
    { name: `${res}月去哪`, key: "002" },
    { name: "露营", key: "003" },
    { name: "古镇漫游", key: "004" },
    { name: "徒步骑行", key: "005" },
    { name: "躺酒店", key: "006" },
    { name: "宝藏小城", key: "007" },
    { name: "亲子游", key: "008" },
    { name: "博物馆", key: "009" },
    { name: "摄影", key: "010" },
    { name: "海景", key: "011" },
  ];
  ctx.send("SUCCESS", 200, arr);
});

//登录校验
router.get("/check-login", new Auth().m, async (ctx) => {
  ctx.send();
});

//根据游记关键词查询游记
router.get("/clAssifyTravels", async (ctx) => {
  const { keywords, page } = ctx.query;
  Classifytravels(keywords, page);
  const query = { $regex: keywords, $options: "i" };
  const res = await modelArticle.aggregate([
    {
      $match: {
        $or: [
          { title: query },
          { content: query },
          { address: query },
          { tags: query },
        ],
      },
    },
    { $skip: (page - 1) * 6 },
    { $limit: 6 },
    looKup().model_user,
    looKup().model_like,
    looKup().projectcard,
  ]);

  ctx.send("SUCCESS", 200, res);
});
module.exports = router.routes();
